﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

namespace FancyIdea.PingShui.Core.Dal
{
    public class CmdUtil
    {
        public static bool ExcuteNonQuery(string sql)
        {
            using (OracleCmd cmd = new OracleCmd())
            {
                cmd.SetCommand(sql);
                try
                {
                    return cmd.ExecuteNonQuery() > 0;
                }
                catch
                {
                    return false;
                }
            }
        }

        public static DataTable ExcuteDataTable(string sql)
        {
            using (OracleCmd cmd = new OracleCmd())
            {
                cmd.SetCommand(sql);
                try
                {
                    return cmd.ExecuteDataTable();
                }
                catch
                {
                    return null;
                }
            }
        }

        public static DataRow ExcuteDataRow(string sql)
        {
            using (OracleCmd cmd = new OracleCmd())
            {
                cmd.SetCommand(sql);
                try
                {
                    return cmd.ExecuteDataRow();
                }
                catch
                {
                    return null;
                }
            }
        }

        public static object ExcuteScalar(string sql)
        {
            using (OracleCmd cmd = new OracleCmd())
            {
                cmd.SetCommand(sql);
                try
                {
                    return cmd.ExecuteScalar();
                }
                catch
                {
                    return null;
                }
            }
        }

        public static bool DeleteMuti(string tableName, string colName, string[] ids)
        {
            if (ids == null || ids.Length < 1) return false;
            using (OracleCmd cmd = new OracleCmd())
            {
                cmd.BeginTransaction();
                for (int i = 0; i < ids.Length; i++)
                {
                    string sql = @"delete from " + tableName + " where " + colName + " ='" + ids[i] + "'";
                    cmd.SetCommand(sql);
                    if (cmd.ExecuteNonQuery() <= 0)
                    {
                        cmd.RollbackTransaction();
                        return false;
                    }
                }
                cmd.CommitTransaction();
                return true;
            }
        }
    }
}